# https://stackoverflow.com/questions/18246309/python-what-type-is-flask-request-files-stream-supposed-to-be
# https://stackoverflow.com/questions/28200366/python-3-x-importerror-no-module-named-cstringio
import time

import requests
from flask import Flask, request, Response, stream_with_context
from io import BytesIO

app = Flask('test')


@app.route("/", methods=['POST'])
def index():
    if request.method == 'POST':
        f = request.files['file']
        print(type(f))
        print(f.content_length)
        # print(f.seek())
        print(f.tell())
        filename = f.filename
        kwargs = {}
        kwargs["user.name"] = "app"
        kwargs["op"] = "CREATE"
        kwargs["overwrite"] = False

        requests_session = requests.api
        # 上传文件
        response = requests_session.request("put",
                                            'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/meta/{}'.format(
                                                filename),
                                            params=kwargs,
                                            timeout=30,
                                            allow_redirects=False)

        print(response.status_code)
        print(response.headers)
        response = requests_session.put(response.headers['location'], data=f)
        print(response.status_code)
        print(response.headers)
        print(f.tell())

    return ""


@app.route("/down_file")
def down_file():
    # https://zdyxry.github.io/2018/07/08/Flask-%E6%B5%81%E5%BC%8F%E5%93%8D%E5%BA%94/
    import pyhdfs
    from pyhdfs import HdfsClient
    import shutil
    client = HdfsClient(hosts="hadoop2x-01:50070,hadoop2x-02:50070", user_name="qhy")
    content_type = 'application/octet-stream'

    # client.copy_to_local(srcFile, dstFile, overwrite=True)
    def generate():
        # create and return your data in small parts here
        with client.open("/BaseData/rule/once/1_24/00002_meta_20200224_000.db3", overwrite=True) as fsrc:
            length = 16 * 1024
            while 1:
                buf = fsrc.read(length)
                if not buf:
                    break
                yield buf

    response = Response(stream_with_context(generate()))
    response.headers['Content-Type'] = content_type
    response.headers['Content-Disposition'] = 'attachment; filename=test.db3'
    return response

#
# def send_file(client, name):
#     print("**************")
#     with open(name, 'rb') as f:
#         client.post('/', data={'file': (BytesIO(f.read()), name)})
#
#
# if __name__ == "__main__":
#     with app.test_client() as client:
#         send_file(client, './zlf_wanfangstandard_20200106.db3')
        # send_file(client, 'megacat-2.png')
